package com.renwei.yunlong.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.renwei.yunlong.R;
import com.renwei.yunlong.bean.KnowCommentBean;
import com.renwei.yunlong.http.Api;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;
import com.zyf.fwms.commonlibrary.utils.GlideCircleTransform;

import butterknife.Bind;
import butterknife.ButterKnife;
import io.github.xudaojie.qrcodelib.zxing.DensityUtils;

/**
 * Created by wangqi on 2018/4/16.
 */

public class KnowCommentAdapter extends BaseRecyclerViewAdapter<KnowCommentBean.RowsBean> {

    private String companyType;

    public KnowCommentAdapter(Context context, String companyType) {
        super(context);
        this.companyType = companyType;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public int getItemCount() {
        return mList != null ? mList.size() + 1 : 1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new HeadViewHolder(inflater.inflate(R.layout.view_know_commetn_head, parent, false));
        } else {
            return new CommentViewHolder(inflater.inflate(R.layout.item_know_comment, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        if (getItemViewType(position) == 0) {
            final HeadViewHolder headViewHolder = (HeadViewHolder) holder;
            headViewHolder.tvOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String s = headViewHolder.tvOrder.getText().toString();
                    if ("按热度查看".equals(s)) {
                        headViewHolder.tvOrder.setText("按时间查看");
                    } else if ("按时间查看".equals(s)) {
                        headViewHolder.tvOrder.setText("按热度查看");
                    }
                    if (mOnItemClickLitener != null)
                        mOnItemClickLitener.onItemClick(view, position);
                }
            });
        } else {
            final CommentViewHolder commentViewHolder = (CommentViewHolder) holder;
            final KnowCommentBean.RowsBean bean = getItem(position - 1);
            if (bean != null) {
                Glide.with(mContext).load(("1".equals(companyType) ? Api.$().OWNER_FILE_URL : Api.$().SERVICE_FILE_URL) + bean.getAvatar())
                        .transform(new GlideCircleTransform(mContext)).error(R.mipmap.icon_header_default).into(commentViewHolder.ivHead);
                commentViewHolder.tvName.setText(bean.getRecordUserName());
                commentViewHolder.tvContent.setText(bean.getRecordContent());
                commentViewHolder.tvTime.setText(bean.getRecordTime().replace("T", " "));

                if (TextUtils.isEmpty(bean.getUdstate())) {
                    commentViewHolder.ivUp.setImageResource(R.mipmap.icon_know_like_nor);
                    commentViewHolder.tvUp.setTextColor(mContext.getResources().getColor(R.color.color_808080));
                } else {
                    commentViewHolder.ivUp.setImageResource(R.mipmap.icon_know_like_pre);
                    commentViewHolder.tvUp.setTextColor(mContext.getResources().getColor(R.color.color_ff788b));
                }
                commentViewHolder.tvUp.setText(bean.getUpNum() + "");

                KnowCommentBean.RowsBean.RecodeBean recode = bean.getRecode();
                commentViewHolder.llInfo.removeAllViews();
                if (recode != null) {
                    commentViewHolder.llInfo.setVisibility(View.VISIBLE);
                    TextView textView = new TextView(mContext);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
                            (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    textView.setLayoutParams(params);
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
                    textView.setTextColor(Color.parseColor("#4d4d4d"));
                    textView.setPadding(DensityUtils.dp2px(mContext, 10f), DensityUtils.dp2px(mContext, 10f)
                            , DensityUtils.dp2px(mContext, 10f), DensityUtils.dp2px(mContext, 10f));
                    textView.setBackgroundColor(mContext.getResources().getColor(R.color.bg_color));
                    textView.setLineSpacing(0f, 1.2f);

                    String content = "@" + recode.getRecordUserName() + "：" +
                            ("0".equals(recode.getIsDelete())?recode.getRecordContent():("该条评论已被"+" \""+recode.getDeleteUserName()+"\" 删除"));
                    textView.setText(content);

                    commentViewHolder.llInfo.addView(textView);
                } else {
                    commentViewHolder.llInfo.setVisibility(View.GONE);
                }

                commentViewHolder.tvReply.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (mOnItemClickLitener != null)
                            mOnItemClickLitener.onItemClick(view, position - 1);
                    }
                });
                commentViewHolder.llUp.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (TextUtils.isEmpty(bean.getUdstate())) {
                            bean.setUdstate("1");
                            bean.setUpNum(bean.getUpNum() + 1);
                        } else {
                            bean.setUdstate("");
                            bean.setUpNum(bean.getUpNum() - 1);
                        }
                        notifyItemChanged(position);

                        if (mOnItemClickLitener != null)
                            mOnItemClickLitener.onItemClick(view, position - 1);
                    }
                });
            }
        }
    }

    class HeadViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_order)
        TextView tvOrder;

        public HeadViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class CommentViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.iv_head)
        ImageView ivHead;
        @Bind(R.id.iv_up)
        ImageView ivUp;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_content)
        TextView tvContent;
        @Bind(R.id.ll_info)
        LinearLayout llInfo;
        @Bind(R.id.ll_up)
        LinearLayout llUp;
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.tv_reply)
        TextView tvReply;
        @Bind(R.id.tv_up)
        TextView tvUp;

        public CommentViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
